﻿namespace EyeOpen.TimeAssistant.UI.Silverlight
{
    using EyeOpen.ViewModel;
    using Microsoft.Practices.Composite.Events;
    using Microsoft.Practices.ServiceLocation;

    public class DialogManager
    {
        private ErrorWindow errorWindow;
        private LoginForm loginWindow;

        /// <summary>
        /// Initializes a new instance of the <see cref="DialogManager"/> class.
        /// </summary>
        public DialogManager()
        {
            TheAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();

            TheAggregator.
                GetEvent<HandleLoginWindowEvent>().
                Subscribe(HandleLoginWindow);

            TheAggregator.
                GetEvent<HandleErrorWindowEvent>().
                Subscribe(HandleErrorWindow);
        }

        /// <summary>
        /// Gets or sets the aggregator.
        /// </summary>
        /// <value>The aggregator.</value>
        public IEventAggregator TheAggregator
        {
            get;
            set;
        }

        public void HandleLoginWindow(HandleWindowMessage message)
        {
            if (message.MustCloseWindow)
            {
                loginWindow.DialogResult = true;
            }
            else
            {
                loginWindow = new LoginForm();
                loginWindow.Show();
            }
        }

        public void HandleErrorWindow(HandleWindowMessage message)
        {
            if (message.MustCloseWindow)
            {
                errorWindow.DialogResult = true;
            }
            else
            {
                errorWindow = new ErrorWindow();
                errorWindow.Show();
            }
        }
    }
}
